package com.ufox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.ProdDetails;
import com.ufox.entity.Products;
import com.ufox.entity.Result;
import com.ufox.mapper.ProdDetailsMapper;
import com.ufox.mapper.ProductsMapper;
import com.ufox.req.ProductsCondREQ;
import com.ufox.service.IProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private ProdDetailsMapper prodDetailsMapper;

    @Override
    public boolean findProdById(String id) {
        int rows = productsMapper.findProdById(id);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Page<Products> selectAllByCond(Page<Products> pageInfo, ProductsCondREQ productsCond) {
        return productsMapper.selectAllByCond(pageInfo, productsCond);
    }


    @Override
    public Page<Products> findProdByUser(String id, Page<Products> pageInfo, ProductsCondREQ productsCond) {
        return productsMapper.findProdByUser(id, pageInfo, productsCond);
    }

    @Override
    public boolean updateProdStateById(String state, String id) {
        int rows = productsMapper.updateProdStateById(state, id);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Result saveAndUpdate(Products products) {
        ProdDetails prodDetails = new ProdDetails();
        if (products.getId() == null) {
            productsMapper.insert(products);
            prodDetails.setProdId(products.getId());
            prodDetails.setDetails(products.getDetails());
            prodDetails.setHtmlDetails(products.getHtmlDetails());
            prodDetailsMapper.insert(prodDetails);
        } else {
            productsMapper.updateProdById(products);
            prodDetails.setDetails(products.getDetails());
            prodDetails.setHtmlDetails(products.getHtmlDetails());
            prodDetails.setProdId(products.getId());
            prodDetailsMapper.updateByProdId(prodDetails);
        }

        return Result.ok();
    }

    @Override
    public Result selectProductsByTypeId(IPage<Products> pageInfo, String typeid) {
        productsMapper.selectByTypeIdPage(pageInfo,typeid);
        return Result.ok(pageInfo);
    }

    @Override
    public Result selectDetailById(String id) {
        Products products = productsMapper.selectDetailById(id);
        return Result.ok(products);
    }

    @Override
    public Result selectProductsByName(IPage<Products> pageInfo, String name) {
        productsMapper.selectProductsByName(pageInfo,name);
        return Result.ok(pageInfo);
    }
}
